package myky.com.doctorapp.ui

import android.support.v7.widget.LinearLayoutManager
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.blankj.utilcode.util.KeyboardUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.github.nukc.stateview.Constant
import com.gyf.barlibrary.ImmersionBar
import kotlinx.android.synthetic.main.activity_find_doctor_search.*
import kotlinx.android.synthetic.main.public_common_title.*
import myky.com.doctorapp.R
import myky.com.doctorapp.adapter.SearchDiseaseResultAdapter
import myky.com.doctorapp.adapter.SearchHistoryAdapter
import myky.com.doctorapp.entity.SearchHistoryItem
import myky.com.doctorapp.base.BaseActivity
import myky.com.doctorapp.contract.FindDoctorSeachContract
import myky.com.doctorapp.entity.QuikSearchEntity
import myky.com.doctorapp.presenter.FindDoctorSeachPresenter
import myky.com.doctorapp.utils.ToastUtils

class FindDoctorSearchActivity : BaseActivity<FindDoctorSeachPresenter, List<String>>(), FindDoctorSeachContract.View, BaseQuickAdapter.OnItemChildClickListener, View.OnClickListener, SearchHistoryAdapter.OnTagClicListener {
    override fun onTagClick(seachContent: String) {
        ToastUtils.show(seachContent)

    }

    override fun deleteCallBack(id: Int, postion: Int) {
        ToastUtils.show("刪除第${postion}项")
    }

    lateinit var historyItems: ArrayList<SearchHistoryItem>
    lateinit var searchHistoryAdapter: SearchHistoryAdapter

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.seachLayout -> {
                KeyboardUtils.hideSoftInput(this)
                if (act_search_et_search.text.isNotEmpty()) {
                    mPresenter.findReleseContent(act_search_et_search.text.toString(), seachType)
                }
            }
            R.id.act_search_clearinput -> {
                act_search_et_search.setText("")
            }
        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        KeyboardUtils.hideSoftInput(this)
        val data = searchDiseaseResultAdapter.data

        val inputString = data[position].resultWord

        ToastUtils.show(inputString)



    }

    var seachType: Int = 0
    private lateinit var searchDiseaseEntities: ArrayList<QuikSearchEntity>
    private lateinit var searchDiseaseResultAdapter: SearchDiseaseResultAdapter
    override fun showReleaseContent(data: List<String>) {
        searchDiseaseEntities.clear()
        if (data.isNotEmpty()) {
            searchTips.visibility = View.VISIBLE
            historyLayout.visibility = View.GONE

            for (value in data) {
                val quikSearchEntity = QuikSearchEntity()
                quikSearchEntity.resultWord = value
                quikSearchEntity.keyword = act_search_et_search.text.toString()
                searchDiseaseEntities.add(quikSearchEntity)
            }
            searchDiseaseResultAdapter.notifyDataSetChanged()
        } else {
            historyLayout.visibility = View.VISIBLE
            searchTips.visibility = View.GONE
        }

    }

    override fun reloadActivity() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getPresenter(): FindDoctorSeachPresenter {
        return FindDoctorSeachPresenter()
    }

    override fun injectTarget(): View = findViewById(R.id.injectView)

    override fun initData() {

        historyItems = ArrayList()

        searchDiseaseEntities = ArrayList()

        searchHistoryAdapter = SearchHistoryAdapter(this,historyItems)

        searchDiseaseResultAdapter = SearchDiseaseResultAdapter(searchDiseaseEntities)

        listView.layoutManager = LinearLayoutManager(this)

        listView.adapter = searchDiseaseResultAdapter

        id_flowlayout.adapter = searchHistoryAdapter

        val extras = intent.extras

        seachType = extras.getInt(Constant.SEACHERTYPE)
        val string = extras.getString(Constant.FINDDOCTORSEARCHHINT)
        val searchCode = extras.getInt(Constant.SEACHCODEKEY)
        val titleString = extras.getString(Constant.FINDDOCTORTITLE)
        conmonTitleTextView.text = titleString
        act_search_et_search.hint = string
        searchHistoryAdapter.setOnTagClicListener(this)


        fakeHistory()


        act_search_et_search.setOnEditorActionListener(TextView.OnEditorActionListener { v, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                KeyboardUtils.hideSoftInput(this)
                mPresenter.findReleseContent(v.text.toString(), seachType)
                return@OnEditorActionListener true
            }
            false
        })

        act_search_et_search.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun afterTextChanged(p0: Editable?) {
                if (act_search_et_search.text.isNotEmpty()) {
                    act_search_clearinput.visibility = View.VISIBLE
                    mPresenter.findReleseContent(act_search_et_search.text.toString(), seachType)
                } else {
                    historyLayout.visibility = View.VISIBLE
                    searchTips.visibility = View.GONE
                    act_search_clearinput.visibility = View.GONE
                }
            }
        })
        searchDiseaseResultAdapter.onItemChildClickListener = this

        seachLayout.setOnClickListener(this)
        act_search_clearinput.setOnClickListener(this)
    }

    private fun fakeHistory() {


        for (i in 1..10) {

            var s = SearchHistoryItem(1, "太帅了")

            historyItems.add(s)
        }
        searchHistoryAdapter.notifyDataChanged()

    }

    override fun initView() {
        ImmersionBar.with(this)
                .statusBarColor(R.color.colorPrimary)
                .titleBar(R.id.commonTitleLayout)
                .init()
    }

    override fun getContenView(): Int = R.layout.activity_find_doctor_search

    override fun showDataSuccess(datas: List<String>?) {
    }

}
